package cn.itcast.ssh.utils;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.itcast.ssh.domain.Employee;
import cn.itcast.ssh.service.IEmployeeService;

/**
 * 员工经理任务分配
 *
 */
@SuppressWarnings("serial")
public class ManagerTaskHandler implements TaskListener {

	@Override
	public void notify(DelegateTask delegateTask) {
		//获取当前用户
//		Employee employee = SessionContext.get();
//		//懒加载导致Manager是空的,这里会报空指针异常
//		String name = employee.getManager().getName();
		/*1.重新查询当前用户,再获取当前用户的领导*/
		Employee employee = SessionContext.get();
		//2.获取当前用户的名字
		String name = employee.getName();
		//3.使用当前用户名字查询当前用户的详细信息
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
		//<bean id="employeeService" class="cn.itcast.ssh.service.impl.EmployeeServiceImpl">
		IEmployeeService employeeService = (IEmployeeService) ac.getBean("employeeService");
		employee = employeeService.findEmployeeByName(name);
		//指定下一个任务的执行人为当前用户的领导
		delegateTask.setAssignee(employee.getManager().getName());
	}

}
